package com.mdd.admin.service.pay.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.mdd.admin.service.pay.IDevPayService;
import com.mdd.admin.validate.pay.DevPayParam;
import com.mdd.admin.vo.pay.DevPayConfigVo;
import com.mdd.admin.vo.pay.DevPayDetailVo;
import com.mdd.admin.vo.pay.DevPayListVo;
import com.mdd.common.entity.pay.DevPay;
import com.mdd.common.enums.OrderPlayTypeEnum;
import com.mdd.common.mapper.pay.DevPayMapper;
import com.mdd.common.utils.UrlUtil;
import com.mdd.common.utils.YmlUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * 支付配置实现类
 */
@Service
public class DevPayServiceImpl extends ServiceImpl<DevPayMapper, DevPay> implements IDevPayService {

    @Resource
    DevPayMapper devPayMapper;

    /**
     * 支付配置列表
     *
     * @return PageResult<DevPayListVo>
     */
    @Override
    public List<DevPayListVo> listPage() {
        LambdaQueryWrapper<DevPay> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(DevPay::getId);
        List<DevPay> list = super.list(lambdaQueryWrapper);
        if (CollectionUtils.isEmpty(list)) {
            return Lists.newArrayList();
        }
        List<DevPayListVo> targetList = Lists.newArrayList();
        Map<Integer, String> map = OrderPlayTypeEnum.getMap();
        for (DevPay devPay : list) {
            DevPayListVo vo = new DevPayListVo();
            BeanUtils.copyProperties(devPay, vo);
            vo.setPayWayDesc(map.get(devPay.getPayWay()));
            vo.setImage(UrlUtil.toAbsoluteUrl(devPay.getImage()));
            targetList.add(vo);
        }
        return targetList;
    }

    /**
     * 支付配置详情
     *
     * @param id 主键参数
     * @return DevPay
     */
    @Override
    public DevPayDetailVo detail(Long id) {
        DevPay model = devPayMapper.selectOne(
                new QueryWrapper<DevPay>()
                        .eq("id", id)
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在");

        String env = YmlUtil.get("like.production");
        boolean envStatus = StringUtils.isNotEmpty(env) && env.equals("true");
        DevPayDetailVo vo = new DevPayDetailVo();
        BeanUtils.copyProperties(model, vo);
        if(null != model.getConfig()){
            String config = model.getConfig();
            DevPayConfigVo devPayConfigVo = JSON.parseObject(config, DevPayConfigVo.class);
            devPayConfigVo.setApiclientKey(envStatus ? "******" : devPayConfigVo.getApiclientKey());
            devPayConfigVo.setApiclientCert(envStatus ? "******" : devPayConfigVo.getApiclientCert());
            devPayConfigVo.setPaySignKey(envStatus ? "******" : devPayConfigVo.getPaySignKey());
            devPayConfigVo.setMchId(envStatus ? "******" : devPayConfigVo.getMchId());
            vo.setConfig(devPayConfigVo);
        }
        vo.setImage(UrlUtil.toAbsoluteUrl(model.getImage()));
        vo.setPayWayDesc(OrderPlayTypeEnum.getMap().get(model.getPayWay()));
        return vo;
    }

    /**
     * 支付配置编辑
     *
     * @param devPayParam 参数
     */
    @Override
    public void edit(DevPayParam devPayParam) throws Exception {
        DevPay model = devPayMapper.selectOne(
                new QueryWrapper<DevPay>()
                        .eq("id", devPayParam.getId())
                        .last("limit 1"));

        Assert.notNull(model, "数据不存在!");

        model.setId(devPayParam.getId());
        model.setName(devPayParam.getName());
        model.setPayWay(devPayParam.getPayWay());
        String config = JSON.toJSONString(devPayParam.getConfig());
        model.setConfig(config);
        model.setImage(UrlUtil.toRelativeUrl(devPayParam.getImage()));
        model.setSort(devPayParam.getSort());
        devPayMapper.updateById(model);
        //wxPayConfigBuilder.buildWxPayClient();
    }
}
